<?php

namespace app\api\controller;

use app\admin\model\Organization AS OrganizationMdl;
use app\admin\model\Member;

class Organization extends BaseController
{
    /**
     * 组织列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId不能为空');
        }

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }
        $where['townId'] = $param->townId;
        $where['isDelete'] = 2;
        if(!empty($param->villageId)) {
            $where['villageId'] = $param->villageId;
        }

        $list = OrganizationMdl::where($where)->order('sorting ASC,villageId ASC')->limit($start, $length)->select();
        $total = OrganizationMdl::where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到组织信息');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    /**
     * 组织下的成员列表
     */
    public function partyList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->organizationId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'organizationId 不能为空');
        }

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'organizationId' => $param->organizationId,
            'shenheStatus' => ['in', [0, 1]],
            'isDelete' => 2
        ];
        $list = Member::where($where)->limit($start, $length)->order('sorting ASC,createDate DESC')->select();
        $total = Member::where($where)->count();
        /*foreach ($list as &$item) {
            $item['avatar'] = $item['avatar'].'?imageView2/1/w/110/h/110';
        }*/

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到成员信息');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    /**
     * 组织及下面的人员列表
     */
    public function getAllOrganizationMembers()
    {
        $param = self::getHttpParam();

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        $where = [
            'townId' => $param->townId,
            'isDelete' => 2
        ];

        $model = new OrganizationMdl();
        $fields = 'id, name';
        $order = 'sorting ASC, createDate DESC';
        $list = $model->where($where)->field($fields)->order($order)->select();
        if (!$list) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到组织成员信息');
        }

        $data = [];
        foreach ($list as $key => $item) {
            $members = Member::where('organizationId', $item['id'])->field('id, name, avatar, userId, job')->select();
            if ($members) {
                $item['members'] = $members;
                $data[] = $item;
            }
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

}